<?php

class WebUser extends CWebUser {

    private $_model = null;

    public function getModel($id = null) {
        if($this->_model === null) {
            $this->_model = User::model()->findByPk($id == null ? $this->id : $id);
        }
        return $this->_model;
    }

    public function getRole($id = null) {
        $id = $id ? $id : Y::userId();
        if($id == 0) {
            return User::ROLE_GUEST;
        }
        $user = $this->getModel();
        return $user->role;
    }

    /**
     * Admin
     * @param null $id
     * @return bool
     */
    public function isAdmin($id = null) {
        $role = $this->getRole($id);
        return $role == User::ROLE_ADMIN ? true : false;
    }

    /**
     * Moderator is Admin & Moderator
     * @param null $id
     * @return bool
     */
    public function isModerator($id = null) {
        $role = $this->getRole($id);
        return $role == User::ROLE_MODERATOR || $role == User::ROLE_ADMIN ? true : false;
    }
}
